package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayTypeVo;
import org.dromara.system.domain.bo.PayTypeBo;
import org.dromara.system.service.IPayTypeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 支付平台
 * 前端访问路由地址为:/system/PayType
 *
 * @author Gavino
 * @date 2024-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/PayType")
public class PayTypeController extends BaseController {

    private final IPayTypeService payTypeService;

    /**
     * 查询支付平台列表
     */
    @GetMapping("/selectList")
    public R<?> selectList(PayTypeBo bo) {
        bo.setStatus(1);
        return R.ok(payTypeService.queryList(bo));
    }

    /**
     * 查询支付平台列表
     */
    @GetMapping("/list")
    public TableDataInfo<PayTypeVo> list(PayTypeBo bo, PageQuery pageQuery) {
        return payTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出支付平台列表
     */
    @SaCheckPermission("system:PayType:export")
    @Log(title = "支付平台", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayTypeBo bo, HttpServletResponse response) {
        List<PayTypeVo> list = payTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "支付平台", PayTypeVo.class, response);
    }

    /**
     * 获取支付平台详细信息
     *
     * @param typeId 主键
     */
    @SaCheckPermission("system:PayType:query")
    @GetMapping("/{typeId}")
    public R<PayTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long typeId) {
        return R.ok(payTypeService.queryById(typeId));
    }

    /**
     * 新增支付平台
     */
    @SaCheckPermission("system:PayType:add")
    @Log(title = "支付平台", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayTypeBo bo) {
        return toAjax(payTypeService.insertByBo(bo));
    }

    /**
     * 修改支付平台
     */
    @SaCheckPermission("system:PayType:edit")
    @Log(title = "支付平台", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayTypeBo bo) {
        return toAjax(payTypeService.updateByBo(bo));
    }

    /**
     * 删除支付平台
     *
     * @param typeIds 主键串
     */
    @SaCheckPermission("system:PayType:remove")
    @Log(title = "支付平台", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] typeIds) {
        return toAjax(payTypeService.deleteWithValidByIds(List.of(typeIds), true));
    }
}
